package com.hxzy.servlet;

import com.hxzy.bean.Student;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * @author nick
 */
@WebServlet("/data")
public class MyDataServlet extends HttpServlet {
    //浏览器默认请求方式为GET

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //request请求作用域和页面转发
        List<Student> students = Arrays.asList(
                new Student(1, "张三1", 18),
                new Student(2, "张三2", 19),
                new Student(3, "张三3", 20),
                new Student(4, "张三4", 21),
                new Student(5, "张三5", 22)
        );

        //将students集合添加到request作用域
        req.setAttribute("students",students); //将集合保存了request对象中的一个背包中

        //页面转发；将当前的request对象传递给另外一个jsp页面
        RequestDispatcher requestDispatcher = req.getRequestDispatcher("/data.jsp");
        requestDispatcher.forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
